var Bmob = require('utils/bmob.js');
Bmob.initialize("bb66fc93d1cd8744dba20ccef9cf22a3", "d910d16120563e40d5398144c885d7dd");

App({
  onLaunch: function () {
    this.userBmobLogin();
    //this.testBmob();
  },
  globalData: {
    userInfo: null,
    userBusiness: null,
    mobilePhoneNumber: null
  },
  userBmobLogin: function (userInfo) {
    var self = this;
    Bmob.User.auth().then(res => {//实例化用户对象 一键登录
      //查询当前用户的店铺情况
      if (self.isNotNullEmptyOrUndefined(Bmob.User.current().mobilePhoneNumber)) {
        self.globalData.mobilePhoneNumber = Bmob.User.current().mobilePhoneNumber;
        var businessQuery = Bmob.Query('Business');
        businessQuery.equalTo("user","==", Bmob.User.current().objectId);
        businessQuery.find().then(result => {
          if (self.isNotNullEmptyOrUndefined(result)) {
            self.globalData.userBusiness = result[0];
          }
          wx.hideLoading();
        }).catch(error => {
          console.log(error);
          wx.hideLoading();
        });
      }
      else {
        wx.hideLoading();
      }

    }).catch(err => {
      console.log(err);
      wx.hideLoading();
    });


    if (userInfo != null && userInfo != undefined) {
      Bmob.User.upInfo(userInfo);
    }
    this.globalData.userInfo = userInfo;
  },
  isNullEmptyOrUndefined: function (res) {
    return res === null || res === undefined || res === '';
  },
  isNotNullEmptyOrUndefined: function (res) {
    return !this.isNullEmptyOrUndefined(res);
  },
  businessApply: function (e) { //点击商家入驻
    var self = this;
    if (self.isNullEmptyOrUndefined(self.globalData.mobilePhoneNumber)) {
      wx.showModal({
        title: '手机未绑定',
        content: '请先绑定手机号码！',
        showCancel: false
      });
    }
    else if (self.isNotNullEmptyOrUndefined(self.globalData.userBusiness)) {
      wx.navigateTo({
        url: '../space/space?businessId=' + self.globalData.userBusiness.objectId,
      });
    }
    else {
      wx.navigateTo({
        url: '../apply/apply',
      })
    }
  },
  testBmob : function(){

    // const user = {
    //   "__type": "Pointer",
    //   "className": "_User",
    //   "objectId": "0ea7252a45"
    // }
    // const query = Bmob.Query('Business');
    // query.set('user', user)
    // query.save().then(res => {
    //   debugger;
      
    // }).catch(err => {
    //   console.log(err)
    // })
    const query = Bmob.Query('Business');
    query.get('ac80ab8310').then(res => {
      debugger
      console.log(res)
      // res.set('cover', '3333')
      // res.save()
    }).catch(err => {
      console.log(err)
    })
  }
})